<html>
<body>
<cffunction name="dumpvar">
  <cfargument name="var" default="blank">
  <cfargument name="abort" default="true">
  <cftry>
	  <cfdump var="#var#">
	<cfcatch>
	  <cfset writeoutput(cfcatch.Message & " " & cfcatch.Detail & " " & cfcatch.TagContext[1].line & " " & cfcatch.stacktrace) />
	</cfcatch>
	</cftry>
	<cfif arguments.abort>
	  <cfabort />
	</cfif>
</cffunction>


<cfset ormService = request.ormService />
<cfset sessionFactory = request.ormService.getSessionFactory() />
<cfset currentSession = "" />
<cfset event = "" />

<cfparam name="url.action" default="">

<cfsavecontent variable="availableActions">
	<hr />
	<h3>Action List</h3>
	<a href="?action=event.list">List Events</a> |
	<a href="?action=event.form">New Event</a> |
	<a href="?action=person.list">List People</a> |
	<a href="?action=person.form">New Person</a>
	<hr />
</cfsavecontent>

<cfswitch expression="#url.action#">
  <!--- 
	EVENT LIST 
	--->
	<cfcase value="event.list">
		<cfset result = ormservice.query("from Events") />
		<cfoutput>#availableActions#</cfoutput>
		<cfoutput>
		   <cfloop from="1" to="#arrayLen(result)#" index="e">
		   		<cfset i = result[e]>
		      <a href="?action=event.form&eventID=#i.getEventId()#">#i.getEventname()# - #dateFormat(i.getEventDate())#</a><br />
		      <cfset personIterator = i.getParticipants().iterator() />
				   <cfloop condition="#personIterator.hasNext()#">
					   <cfset p = personIterator.next() />
				     &nbsp; &nbsp; #p.getFirstname()# - #p.getLastname()#<br />
				   </cfloop>
				   <br />
		   </cfloop>
		</cfoutput>
	</cfcase>

  <!--- 
	EVENT FORM 
	--->
	<cfcase value="event.form">
		<cfparam name="url.eventId" default="">
		<cfscript>
			event = ormService.load("appexample.data.Events",url.eventId);
		</cfscript>
		<cfoutput>#availableActions#</cfoutput>
		<form action="?action=event.save" method="post">

			<input type="hidden" name="eventID" value="<cfoutput>#val(event.getEventId())#</cfoutput>" />
			<h3>Event</h3>
			Event Name: <input type="text" name="eventname" value="<cfoutput>#event.getEventname()#</cfoutput>" /><br />
			Event Date: <input type="text" name="eventdate" value="<cfoutput>#event.getEventdate()#</cfoutput>"/><br />		

			<h3>People</h3>
			<cfset personIterator = event.getParticipants().iterator() />
			<cfloop condition="#personIterator.hasNext()#">
				<cfset p = personIterator.next() />
				&nbsp; &nbsp; 
				<cfoutput>#p.getFirstname()# - #p.getLastname()#
				<input type="submit" name="removePersonId" value="#p.getPersonId()#"><--- click to remove person
				</cfoutput>
				<br />
			</cfloop>

			<h3>New Person</h3>			
			<cfset peopleArray = ormservice.query("from People") />
			<cfif arrayLen(peopleArray) gt 0>
			Select Existing: 
			<select name="personId">
			  <option value=""></option>
		   <cfloop from="1" to="#arrayLen(peopleArray)#" index="p">
		   		<cfset i = peopleArray[p]>
				   <cfoutput>
			      <option value="#i.getPersonId()#">#i.getFirstName()# - #i.getLastName()#</option>
					 </cfoutput>
			   </cfloop>
			</select>
			<br/>
			&nbsp; &nbsp; <strong>or enter new:</strong><br />
			</cfif>
			First Name: <input type="text" name="firstname" value="" /><br />
			Last Name: <input type="text" name="lastname" value=""/><br />		

			<input type="submit" value="Save">

		</form>
	</cfcase>

  <!--- 
	EVENT SAVE 
	--->
	<cfcase value="event.save">
    <cfscript>
			try {
		 		currentSession = sessionFactory.openSession();
        currentSession.beginTransaction();

				event = ormService.create("appexample.data.Events").newInstance();
				if(structKeyExists(form,"eventId")) {
					currentSession.load( event, form.eventId );
				}
		    event.setEventname(form.eventname);
		    event.setEventdate(createOdbcDateTime(form.eventdate));

				if(structKeyExists(form,"firstname") AND form.firstname gt "") {
					person = ormService.create("appexample.data.People").newInstance();
			    person.setFirstname(form.firstname);
			    person.setLastname(form.lastname);
			    person.getEvents().add(event);
			    event.getParticipants().add(person);
			    currentSession.saveOrUpdate(person);
				}
				if(structKeyExists(form,"removePersonId")) {
					person = ormService.create("appexample.data.People").newInstance();
					currentSession.load( person, javacast("int",form.removePersonId) );
			    person.getEvents().remove(event);
			    event.getParticipants().remove(person);
			    currentSession.saveOrUpdate(person);
				}

		    currentSession.flush();
        currentSession.getTransaction().commit();

			} 
			catch(Any excpt) {
				err = structNew();
				err["type"] = excpt.type;
				err["Message"] = excpt.Message;
				err["Detail"] = excpt.Detail;
				err["TagContext"] = excpt.TagContext;
				err["ErrorCode"] = excpt.ErrorCode;
				err["ExtendedInfo"] = excpt.ExtendedInfo;

				// dump of struct is endless loop do to cf introspection haywireness so just do msg/stack
			  dumpvar(err);
			}
		</cfscript>
		<cfoutput>#availableActions#</cfoutput>
		<h2>Saved Event!</h2>
	</cfcase>


  <!--- 
	PERSON LIST 
	--->
	<cfcase value="person.list">
		<cfset result = ormservice.query("from People") />
		<cfoutput>#availableActions#</cfoutput>
		<cfoutput>
		   <cfloop from="1" to="#arrayLen(result)#" index="p">
		   		<cfset i = result[p]>
			   <cfoutput>
		      <a href="?action=person.form&personID=#i.getPersonId()#">#i.getFirstName()# - #i.getLastName()#</a><br />
				 </cfoutput>
		   </cfloop>
		</cfoutput>
	</cfcase>

  <!--- 
	PERSON SAVE 
	--->
	<cfcase value="person.save">
    <cfscript>
			try {
		 		currentSession = sessionFactory.openSession();
        currentSession.beginTransaction();

				person = ormService.create("appexample.data.People").newInstance();
				if(structKeyExists(form,"personId") AND form.personID gt "") {
					currentSession.load( person, javacast("int",form.personId) );
				}
		    person.setFirstname(form.firstname);
		    person.setLastname(form.lastname);

		    currentSession.saveOrUpdate(person);

		    currentSession.flush();
        currentSession.getTransaction().commit();

			} 
			catch(Any excpt) {
				// dump of struct is endless loop do to cf introspection haywireness so just do msg/stack
			  dumpvar(excpt.message & " " & excpt.stacktrace);
			}
		</cfscript>
		<cfoutput>#availableActions#</cfoutput>
		<h2>Saved Person!</h2>
	</cfcase>

  <!--- 
	DELETE PERSON
	--->
	<cfcase value="person.delete">
		<cfscript>
			try {
		 		currentSession = sessionFactory.openSession();
        currentSession.beginTransaction();
				person = ormService.create("appexample.data.People").newInstance();
				if(structKeyExists(url,"personId")) {
					currentSession.load( person, javacast("int",url.personId) );
				}
        currentSession.delete(person);
        currentSession.getTransaction().commit();
			} 
			catch(Any excpt) {
				// dump of struct is endless loop do to cf introspection haywireness so just do msg/stack
			  dumpvar(excpt.message & " " & excpt.stacktrace);
			}
		</cfscript>
		<h2>Deleted Person!</h2>
		<cfoutput>#availableActions#</cfoutput>
	</cfcase>

  <!--- 
	PERSON FORM 
	--->
	<cfcase value="person.form">
		<cfparam name="url.personId" default="">
		<cfscript>
			person = ormService.load("appexample.data.People",url.personId);
		</cfscript>
		<cfoutput>#availableActions#</cfoutput>
		<form action="?action=person.save" method="post">

			<input type="hidden" name="personId" value="<cfoutput>#person.getPersonId()#</cfoutput>" />
			<h3>New Person</h3>			
			First Name: <input type="text" name="firstname" value="<cfoutput>#person.getFirstname()#</cfoutput>" /><br />
			Last Name: <input type="text" name="lastname" value="<cfoutput>#person.getLastname()#</cfoutput>"/><br />		

			<input type="submit" value="Save">

		</form>
	</cfcase>


	<cfdefaultcase>
		<cfoutput>#availableActions#</cfoutput>
	</cfdefaultcase>
</cfswitch>

</body>
</html>
